<?php
namespace app\home\controller;

use app\common\model\UserPlatoon as UserPlatoonModel;

use app\common\component\user\UserService;
use app\common\component\user_platoon\UserPlatoonService;
use app\common\component\user_platoon_log\UserPlatoonLogService;

class UserPlatoon extends ControllerBase {
	public function _initialize() {
		$this -> checkLogin();
	}

	public function index() {
		$this -> view -> assign('userPlatoonLogs', \app\common\model\UserPlatoonLog::where('to_user_name', \think\Session::get('user_name')) -> order('is_state ASC, user_platoon_log_id DESC') -> paginate(10, true));

		$resultGetUserPlatoon = (new UserPlatoonService) -> getUserPlatoonId(\think\Session::get('user_id'));
      	$this -> view -> assign('user_platoon', $resultGetUserPlatoon['data']['user_platoon']);

		return $this -> view -> fetch();
	}

	public function topUp() {
		$resultUser = (new UserService) -> getUser();
    	$this -> view -> assign('user', $resultUser['data']['user']);

		$resultGetUserPlatoon = (new UserPlatoonService) -> getUserPlatoonId(\think\Session::get('user_id'));
      	$this -> view -> assign('user_platoon', $resultGetUserPlatoon['data']['user_platoon']);

		$this -> view -> assign('PLATOON_EACH_PRICE', getParam('PLATOON_EACH_PRICE'));
		return $this -> view -> fetch();
	}

	public function handleTopUp() {
		$post = $this -> request -> post();

		$userName = \think\Session::get('user_name');

		$resultUserPlatoonLogService = (new UserPlatoonLogService()) -> log(1, 1, $post['price'] / getParam('PLATOON_EACH_PRICE'), $userName, $userName, '排单币充值', 0);

		if($resultUserPlatoonLogService['code'] != 0) {
			echo '<meta charset="utf-8"><script>alert("' . $resultUserPlatoonLogService['message'] . '");window.history.go(-1);</script>';exit;
		}

		echo '<meta charset="utf-8"><script>alert("' . $resultUserPlatoonLogService['message'] . '");window.location.href="/user/platoon";</script>';exit;
	}

	public function transfer() {
		$resultGetUserPlatoon = (new UserPlatoonService) -> getUserPlatoonId(\think\Session::get('user_id'));
      	$this -> view -> assign('user_platoon', $resultGetUserPlatoon['data']['user_platoon']);
		return $this -> view -> fetch();
	}

	public function handleTransfer() {
		$post = $this -> request -> post();
		$fromUserName = \think\Session::get('user_name');

		if($post['to_user_name'] == $fromUserName) {
			echo '<meta charset="utf-8"><script>alert("不能转账给自己!");window.history.go(-1);</script>';exit;
		}

		$resultToUser = \app\common\component\user\UserModel::get(['username' => $post['to_user_name']]);
		if(!$resultToUser) {
			echo '<meta charset="utf-8"><script>alert("用户不存在!");window.history.go(-1);</script>';exit;
		}

		$getUserPlatoon = (new UserPlatoonService) -> getUserPlatoonId( \think\Session::get('user_id'));
		if($getUserPlatoon['data']['user_platoon'] -> price <= 0) {
			echo '<meta charset="utf-8"><script>alert("排单币不足!");window.history.go(-1);</script>';exit;
		}

		(new UserPlatoonService) -> getUserPlatoonId($resultToUser['user_id']);

		\think\Db::startTrans();
		try {
			$resultUserPlatoonLogServiceLog1 = (new UserPlatoonLogService()) -> log(3, 0, $post['price'], $fromUserName, $post['to_user_name'], '排单币转出', 1);
			if($resultUserPlatoonLogServiceLog1['code'] !== 0) {
				throw new \Exception($resultUserPlatoonLogServiceLog1['message']);
			}

			$resultUserPlatoonLogServiceLog2 = (new UserPlatoonLogService()) -> log(4, 1, $post['price'], $post['to_user_name'], $fromUserName, '排单币转入', 1);
			if($resultUserPlatoonLogServiceLog2['code'] !== 0) {
				throw new \Exception($resultUserPlatoonLogServiceLog2['message']);
			}

			if(!UserPlatoonModel::where('user_id', $resultToUser['user_id']) -> setInc('price', $post['price'])) {
				throw new \Exception('转账失败');
			}

			if(!(UserPlatoonModel::where('user_id', \think\Session::get('user_id')) -> setDec('price', $post['price']))) {
				throw new \Exception('转账失败');
			}

			\think\Db::commit();
		} catch(\Exception $e) {
			\think\Db::rollback();

			echo '<meta charset="utf-8"><script>alert("' . $e -> getMessage() . '!");window.history.go(-1);</script>';exit;
		}

		echo '<meta charset="utf-8"><script>alert("转账成功");window.location.href="/user/platoon";</script>';exit;
	}
}